//
//  SelectIdentyPickView.swift
//  Exchange
//
//  Created by 孟祥群 on 2018/4/23.
//  Copyright © 2018年 common. All rights reserved.
//

import UIKit

class SelectIdentyPickView: SelectPickerView, UIPickerViewDataSource, UIPickerViewDelegate {
    lazy var pickView: UIPickerView = {
        let pickView = UIPickerView()
        pickView.backgroundColor = UIColor.clear
        pickView.delegate = self
        pickView.dataSource = self
        return pickView
    }()
    
    override init(style:selectStyle, title:String, selectStr:AnyObject?, dataArr:Array<Any>, dataSource:SlecterProcotol) {
        super.init(style:style, title: title, selectStr:selectStr, dataArr: dataArr, dataSource:dataSource)
        nameArr = dataArr
        if nameArr.count > 0 && (self.selectContent as! String).count == 0{
            self.selectContent = nameArr[0] as! String as AnyObject
        }
        self.setUpUI()
    }
    func setUpUI() -> Void {
        self.addSubview(pickView)
        pickView.snp.makeConstraints { (make) in
            make.top.left.right.bottom.equalToSuperview()
        }
        if (self.selectContent as! String).count != 0 {
            let index = (nameArr as NSArray).index(of: self.selectContent as Any)
            if index != NSNotFound {
                pickView.selectRow(index, inComponent: 0, animated: false)
            }
        }
    }
    
    func numberOfComponents(in pickerView: UIPickerView) -> Int {
        return 1
    }
    
    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        return nameArr.count
    }
    
    func pickerView(_ pickerView: UIPickerView, rowHeightForComponent component: Int) -> CGFloat {
        return 44
    }
    func pickerView(_ pickerView: UIPickerView, attributedTitleForRow row: Int, forComponent component: Int) -> NSAttributedString? {
        let arr = nameArr as NSArray
        //        let titleAttribute = NSDictionary.dic
        let color:UIColor;
        if ThemeManager.default.currentThemeMode() == .night {
            color = UIColor(hexStr: "#616979")
        }else {
            color = UIColor(hexStr: "#001705")
        }
        let attributes = [
            NSAttributedStringKey(rawValue: NSAttributedStringKey.foregroundColor.rawValue):  color
            ] as [NSAttributedStringKey : Any]
        let restr = NSMutableAttributedString(string: (arr.object(at: row) as? String)!, attributes: attributes)
        return restr
    }
    
    func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
        
        self.selectContent = nameArr[row]  as AnyObject
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    /*
    // Only override draw() if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    override func draw(_ rect: CGRect) {
        // Drawing code
    }
    */

}
